package cn.gailvlun.gll.presentation.chat;

import android.app.Activity;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityModifyNickBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.Member;
import cn.gailvlun.gll.net.apartment.member.ModifyMemberReq;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.chat.vm.ModifyNickViewModel;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;



public class ModifyNickActivity extends BaseActivity {

    public static Observable<Result<Activity>> openActivity(Activity context, int roomId) {
        Intent intent = new Intent(context, ModifyNickActivity.class);
        intent.putExtra("roomId", roomId);
        return RxActivityResult.on(context)
                .startIntent(intent);
    }

    private ActivityModifyNickBinding mBinding;
    private ModifyNickViewModel mViewModel;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_modify_nick);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.nbv.setOnMenuClickListener(new NavigationBarView.OnMenuClickListener() {
            @Override
            public void onMenuClick(NavigationBarView nb) {
                if (mBinding.etNickName.length() == 0) {
                    ToastUtil.showCenter("不可为空");
                    return;
                }
                ModifyMemberReq modifyMemberReq = new ModifyMemberReq();
                modifyMemberReq.setMember_id(mBinding.getMember().getId());

                modifyMemberReq.setNickname(mBinding.etNickName.getText().toString());

                HttpMethods.getMemberService()
                        .modifyMember(modifyMemberReq)
                        .compose(RxUtil.<Member>applyScheduler())
                        .subscribe(new Consumer<Member>() {
                            @Override
                            public void accept(final Member member) throws Exception {
                                mViewModel.save(member);
                                ToastUtil.showShort("修改成功");
                                setResult(RESULT_OK);
                                finish();
                            }
                        });
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        int roomId = getIntent().getIntExtra("roomId", -1);
        if (roomId == -1) {
            ToastUtil.showShort("出错啦");
            finish();
        }

        mViewModel = ViewModelProviders.of(this).get(ModifyNickViewModel.class);
        Member member = mViewModel.getMyMemberByRoomId(roomId);
        mBinding.setMember(member);
    }






}
